#include "pieframe.h"
#include <QHBoxLayout>
#include <QFrame>
#include "pie.h"
#include <QLabel>
#include "dbutil.h"
#include <iostream>
#include <QFont>
#include<typeinfo>
using namespace std;
PieFrame::PieFrame(QWidget *parent) :
    QWidget(parent)
{
    horizontalLayout_16 = new QHBoxLayout(this);
    horizontalLayout_16->setSpacing(0);
    horizontalLayout_16->setContentsMargins(0, 0, 0, 0);
    horizontalLayout_16->setObjectName(QString::fromUtf8("horizontalLayout_16"));
    pietitle_frame = new QFrame(this);
    pietitle_frame->setObjectName(QString::fromUtf8("pietitle_frame"));
    pieWidget = NULL;
    piealert_frame = new QFrame(this);
    piealert_frame->setObjectName(QString::fromUtf8("piealert_frame"));
    gridLayout_7 = new QGridLayout(piealert_frame);
    gridLayout_7->setContentsMargins(0, 0, 0, 0);
    gridLayout_7->setObjectName(QString::fromUtf8("gridLayout_7"));

    horizontalLayout_16->addWidget(piealert_frame);
    horizontalLayout_16->addWidget(pietitle_frame);
    horizontalLayout_16->setStretch(0, 4);
    horizontalLayout_16->setStretch(1, 2);
    total = 0;
    init();
}
void PieFrame::init()
{
    int y = 28;
    int index = 0;
    int w1 = 40;
    int x1 = 20;
    QFont font8;
    int high = 25;
    font8.setPointSize(10);
    font8.setFamily("SimSun");
    for (int i = 0; i <= 3; i++)
    {
        QLabel *label = new QLabel(this->pietitle_frame);
        label->setGeometry(x1, y, w1, high);
        label->setFont(font8);
        labels.push_back(label);
        QLabel *pic = new QLabel(this->pietitle_frame);
        pic->setFont(font8);
        pic->setScaledContents(false);
        QString col("background-color:");
        col.append(pieWidget->getQColor(index++).toRgb().name());
        col.append(";font-color:white;");
        pic->setStyleSheet(col);
        pic->setAutoFillBackground(true);
        pic->setGeometry(x1 + w1, y, 55, high);
        pic->setAlignment(Qt::AlignCenter);
        labels.push_back(pic);
        y = y + high;
    }
}

void PieFrame::setPieNum(QList<PieObj> m_number)
{

    total = 0;
    if (pieWidget != NULL)
    {
        delete pieWidget;
    }
    pieWidget = new Pie(piealert_frame);
    pieWidget->setObjectName(QString::fromUtf8("pieWidget"));
    gridLayout_7->addWidget(pieWidget, 0, 0, 1, 1);
    if (!m_number.empty())
    {
        foreach(PieObj obj, m_number)
        {
            this->total += obj.getNum();
        }
        QList<double> lstPercent;
        foreach(PieObj obj, m_number)
        {
            lstPercent.append(obj.getNum() * 1.0 / total);
        }
        QFont font8;
        //            font8.setFamily(QString::fromUtf8("Agency FB"));
        font8.setPointSize(10);
        font8.setFamily("SimSun");
        int i = 0;
        foreach(PieObj obj, m_number)
        {
            QLabel *label = labels.at(i++);
            label->setText(obj.getTile() + ":");
            QLabel *pic = labels.at(i++);
            QString txt = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
                          "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
                          "p, li { white-space: pre-wrap; }\n"
                          "</style></head><body style=\" font-family:'SimSun'; font-size:11pt; font-weight:400; font-style:normal;\">\n"
                          "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-weight:600; color:#ffffff;\">%1</span></p></body></html>";
            txt = txt.arg(DbUtils::getStringFormInt(obj.getNum()));
            pic->setText(txt);
        }
        int i_dep = 10;
        //        pieWidget->get
        pieWidget->setPiePos(20, i_dep);
        pieWidget->setPieSize(100, 100);
        pieWidget->setPiePerCent(lstPercent);
        pieWidget->setChartDepth(i_dep);
    }
}
